package com.example.lovedogs.fragment;


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.spec.IvParameterSpec;

import org.json.JSONArray;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.lovedogs.R;
import com.example.lovedogs.domain.Doctor;
import com.example.lovedogs.domain.Friend;
import com.example.lovedogs.friends.ChattingPageActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SearchViewCompat.OnCloseListenerCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class FriendsFragment extends Fragment {
	private LayoutInflater inflater;
	private ScrollView friends_sv;
	private ListView friends_lv1;
	private Button friends_btn_bset;
	private Button friends_btn_doc;
	private Button friends_btn_single;
	private Button search_btn_friends;
	public String btnSelected="friends_btn_doc";
	private String url;
	public RequestQueue rq=null;
	protected MyAdapter_doc adapter_doc;
	protected MyAdapter_fri adapter_fri;
	

	class MyAdapter_doc extends BaseAdapter {
		private List<Doctor> dInf;
		private ImageView iv_doc;
		private TextView tv1_doc;
		private TextView tv2_doc;

		public MyAdapter_doc(List<Doctor> dInf) {
			this.dInf = dInf;
		}

		@Override
		public int getCount() {
			return dInf.size();
		}

		@Override
		public Object getItem(int position) {
			return dInf.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
		public void setData(List<Doctor> dInf) {
			this.dInf=dInf;
		}
//		int image[] = { R.drawable.doctoravatar_01, R.drawable.doctoravatar_02, R.drawable.doctoravatar_03,
//				R.drawable.doctoravatar_04, R.drawable.doctoravatar_05, R.drawable.doctoravatar_06,
//				R.drawable.doctoravatar_07, R.drawable.doctoravatar_08, R.drawable.doctoravatar_09};

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater
						.inflate(R.layout.friends_list_item, null);
			}
			
			iv_doc = (ImageView) convertView.findViewById(R.id.friends_iv_doc);
//			iv_doc.setImageResource(image[position]);
			tv1_doc = (TextView) convertView.findViewById(R.id.friends_tv1_doc);
			tv2_doc = (TextView) convertView.findViewById(R.id.friends_tv2_doc);
			Doctor dinf = dInf.get(position);
			tv1_doc.setText(dinf.getName());
			tv2_doc.setText(dinf.getIntro());
			System.out.println(dinf.getName());
			return convertView;

		}

		

	}
	class MyAdapter_fri extends BaseAdapter {
		private List<Friend> fInf;
		private ImageView iv_doc;
		private TextView tv1_doc;
		private TextView tv2_doc;

		public MyAdapter_fri(List<Friend> fInf) {
			this.fInf = fInf;
		}

		@Override
		public int getCount() {
			return fInf.size();
		}

		@Override
		public Object getItem(int position) {
			return fInf.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

//		int image[] = { R.drawable.doctoravatar_01, R.drawable.doctoravatar_02, R.drawable.doctoravatar_03,
//				R.drawable.doctoravatar_04, R.drawable.doctoravatar_05, R.drawable.doctoravatar_06,
//				R.drawable.doctoravatar_07, R.drawable.doctoravatar_08, R.drawable.doctoravatar_09};

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater
						.inflate(R.layout.friends_list_item, null);
			}
			
			iv_doc = (ImageView) convertView.findViewById(R.id.friends_iv_doc);
//			iv_doc.setImageResource(image[position]);
			tv1_doc = (TextView) convertView.findViewById(R.id.friends_tv1_doc);
			tv2_doc = (TextView) convertView.findViewById(R.id.friends_tv2_doc);
			Friend dInf = fInf.get(position);
			tv1_doc.setText(dInf.getName());
			tv2_doc.setText(dInf.getSign());
			System.out.println(dInf.getName());
			return convertView;

		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.inflater = inflater;
		View rootView = inflater.inflate(R.layout.fragment_friends, container,
				false);
		search_btn_friends=(Button)rootView.findViewById(R.id.search_btn_friends);
		search_btn_friends.setOnClickListener(new listener());
		friends_btn_bset=(Button)rootView.findViewById(R.id.friends_btn_best);
		friends_btn_bset.setOnClickListener(new listener());
		friends_btn_doc=(Button)rootView.findViewById(R.id.friends_btn_doc);
		friends_btn_doc.setOnClickListener(new listener());
		friends_btn_single=(Button)rootView.findViewById(R.id.friends_btn_single);
		friends_btn_single.setOnClickListener(new listener());
		friends_lv1 = (ListView) rootView.findViewById(R.id.friends_lv1);
		friends_sv = (ScrollView) rootView.findViewById(R.id.sv_friends);
		friends_sv.smoothScrollTo(0, 0);
		initListView();
		friends_lv1.setOnItemClickListener(new itemListener());
		
		// friends_lv1=(ListView) rootView.findViewById(R.id.friends_lv1);
//		List<FriendsInf_doc> fInf = new ArrayList<FriendsInf_doc>();
//		fInf.add(new FriendsInf_doc("医生", "好啊哈哈啊"));
//		fInf.add(new FriendsInf_doc("医生2", "qwerqwere"));
//		fInf.add(new FriendsInf_doc("医生3", "德玛西亚"));
//		fInf.add(new FriendsInf_doc("医生4", "诺克萨斯万岁"));
//		fInf.add(new FriendsInf_doc("医生5", "对对赌丢弟弟"));
//		fInf.add(new FriendsInf_doc("医生6", "诺克萨斯万岁"));
//		fInf.add(new FriendsInf_doc("新医生7", "诺克萨斯万岁"));
//		fInf.add(new FriendsInf_doc("新医生8", "德玛西亚"));
//		fInf.add(new FriendsInf_doc("新医生9", "德玛西亚"));
//		MyAdapter_doc adapter_lv1 = new MyAdapter_doc(dInf);
//		friends_lv1.setAdapter(adapter_lv1);
		return rootView;
	}
	class listener implements OnClickListener{

		@Override
		public void onClick(View v) {
			int id=v.getId();
			if (id==R.id.friends_btn_doc) {
				btnSelected="friends_btn_doc";
//				int image2[] = { R.drawable.img01, R.drawable.img01, R.drawable.img01,
//						R.drawable.img01, R.drawable.img01, R.drawable.img01,
//						R.drawable.img01, R.drawable.img01, R.drawable.img01 };
//				List<FriendsInf_doc> fInf = new ArrayList<FriendsInf_doc>();
//				fInf.add(new FriendsInf_doc("医生1", "我是个好医生"));
//				fInf.add(new FriendsInf_doc("医生2", "我也是个好医生"));
//				fInf.add(new FriendsInf_doc("医生3", "我才是好医生"));
//				fInf.add(new FriendsInf_doc("医生4", "大家记得找我啊"));
//				fInf.add(new FriendsInf_doc("医生5", "我专业是宠物心理"));
//				fInf.add(new FriendsInf_doc("医生6", "希望可以帮助到大家"));
//				fInf.add(new FriendsInf_doc("医生7", "大家好"));
//				fInf.add(new FriendsInf_doc("医生8", "这个软件很好用啊，狗狗的看病难看病贵的问题得到解决"));
//				fInf.add(new FriendsInf_doc("医生9", "大家快来关注我啊~~~"));
//
//				MyAdapter_fri adapter_fri = new MyAdapter_fri(fInf);
//				friends_lv1.setAdapter(adapter_fri);
//				rq=Volley.newRequestQueue(getActivity());
				initListView();
			}else if (id==R.id.friends_btn_best) {
				btnSelected="friends_btn_best";
				rq=Volley.newRequestQueue(getActivity());
				initListView();
			}else if (id==R.id.friends_btn_single) {
				btnSelected="friends_btn_single";
				/*List<FriendsInf_doc> fInf = new ArrayList<FriendsInf_doc>();
				fInf.add(new FriendsInf_doc("狗友8", "诺克萨斯万岁"));
				fInf.add(new FriendsInf_doc("狗友9", "诺克萨斯万岁"));
				fInf.add(new FriendsInf_doc("狗友10", "诺克萨斯万岁"));
				fInf.add(new FriendsInf_doc("狗友11", "诺克萨斯万岁"));
				fInf.add(new FriendsInf_doc("狗友12", "诺克萨斯万岁"));
				fInf.add(new FriendsInf_doc("狗友31", "诺克萨斯万岁"));
				fInf.add(new FriendsInf_doc("狗友435", "诺克萨斯万岁"));
				fInf.add(new FriendsInf_doc("狗友8", "德玛西亚"));
				fInf.add(new FriendsInf_doc("狗友9", "德玛西亚"));
				MyAdapter_lv1 adapter_lv1 = new MyAdapter_lv1(fInf);
				friends_lv1.setAdapter(adapter_lv1);*/
				rq=Volley.newRequestQueue(getActivity());
				initListView();
			}else if (id==R.id.search_btn_friends) {
//					Intent intent=new Intent(getActivity(),SearchPageActivity.class);
//					intent.putExtra("btnSelected", btnSelected);
//					startActivity(intent);
			}
			
		}
		
	}
	
	public void initListView(){
//		JsonArrayRequest jar=new JsonArrayRequest(url,new Listener<JSONArray>(){
//
//			@Override
//			public void onResponse(JSONArray array) {
//				String datajson=array.toString();
//				
//				Gson g=new Gson();
				if (btnSelected=="friends_btn_doc") {
					List<Doctor> fInf = new ArrayList<Doctor>();
					fInf.add(new Doctor("医生1","", "我是个好医生"));
					fInf.add(new Doctor("医生2","", "我也是个好医生"));
					fInf.add(new Doctor("医生3","", "我才是好医生"));
					fInf.add(new Doctor("医生4","", "大家记得找我啊"));
					fInf.add(new Doctor("医生5","", "我专业是宠物心理"));
					fInf.add(new Doctor("医生6", "","希望可以帮助到大家"));
					fInf.add(new Doctor("医生7", "","大家好"));
					fInf.add(new Doctor("医生8", "","这个软件很好用啊，狗狗的看病难看病贵的问题得到解决"));
					fInf.add(new Doctor("医生9", "","大家快来关注我啊~~~"));
					/*Type type=new TypeToken<List<Doctor>>(){}.getType();
					List<Doctor> data=g.fromJson(datajson, type);
					
					if (adapter_doc==null) {
						adapter_doc=new MyAdapter_doc(data);
					}else {
						adapter_doc.setData(data);
					}*/
					adapter_doc = new MyAdapter_doc(fInf);
					friends_lv1.setAdapter(adapter_doc);
				}else if (btnSelected=="friends_btn_best") {
					List<Doctor> fInf = new ArrayList<Doctor>();
					fInf.add(new Doctor("关注1","", "我是个好医生"));
					fInf.add(new Doctor("关注2","", "我也是个好关注"));
					fInf.add(new Doctor("关注3","", "我才是好关注"));
					fInf.add(new Doctor("关注4","", "大家记得找我啊"));
					fInf.add(new Doctor("关注5","", "我专业是宠物心理"));
					fInf.add(new Doctor("关注6", "","希望可以帮助到大家"));
					fInf.add(new Doctor("关注7", "","大家好"));
					fInf.add(new Doctor("关注8", "","这个软件很好用啊，狗狗的看病难看病贵的问题得到解决"));
					fInf.add(new Doctor("关注9", "","大家快来关注我啊~~~"));

					adapter_doc = new MyAdapter_doc(fInf);
					friends_lv1.setAdapter(adapter_doc);
				}else if (btnSelected=="friends_btn_single") {
					List<Friend> fInf = new ArrayList<Friend>();
					fInf.add(new Friend("狗友8", "", "我是个好医生"));
					fInf.add(new Friend("狗友8","", "我也是个好关注"));
					fInf.add(new Friend("狗友8","", "我才是好关注"));
					fInf.add(new Friend("关注4","", "大家记得找我啊"));
					fInf.add(new Friend("狗友5","", "我专业是宠物心理"));
					fInf.add(new Friend("狗友6", "","希望可以帮助到大家"));
					fInf.add(new Friend("狗友7", "","大家好"));
					fInf.add(new Friend("狗友8", "","这个软件很好用啊，狗狗的看病难看病贵的问题得到解决"));
					fInf.add(new Friend("狗友9", "","大家快来关注我啊~~~"));

					adapter_fri = new MyAdapter_fri(fInf);
					friends_lv1.setAdapter(adapter_fri);
				}
//			}
//		
//		},new ErrorListener(){
//
//			@Override
//			public void onErrorResponse(VolleyError arg0) {
//				Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
//			}
//			
//		});
//		rq.add(jar);
	}
	class itemListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if (btnSelected=="friends_btn_doc") {
				Intent intent = new Intent(getActivity(),
						ChattingPageActivity.class);
				Doctor docInf =(Doctor) adapter_doc.getItem(position);
				intent.putExtra("Name", docInf.getName());
				intent.putExtra("btnName", btnSelected);
				startActivity(intent);
			}else if (btnSelected=="friends_btn_best") {
				Intent intent = new Intent(getActivity(),
						ChattingPageActivity.class);
				Doctor docInf =(Doctor) adapter_doc.getItem(position);
				intent.putExtra("Name", docInf.getName());
				intent.putExtra("btnName", btnSelected);
				startActivity(intent);
			}else if (btnSelected=="friends_btn_single") {
				Intent intent = new Intent(getActivity(),
						ChattingPageActivity.class);
				Friend friInf =(Friend) adapter_fri.getItem(position);
				intent.putExtra("Name", friInf.getName());
				intent.putExtra("btnName", btnSelected);
				startActivity(intent);
			}
		}
		
	}
}
